/*
 * Copyright (c) Jan Šlahora (${USER}) - 2012.
 * Created in IntelliJ IDEA 10.5
 */

package statistics.Asonance;

import statistics.tests.ATest;
import statistics.tests.Anova;

import java.util.ArrayList;
import java.util.zip.DeflaterOutputStream;

/*
*   File AsonanceVsechnyPosuny 
*   in package statistics.Asonance
*   was created by slaha
*   on 27.3.12 at 20:46.
*
*/
public class AsonanceVsechnyPosuny extends  IAsonance {

  //private ArrayList<ArrayList<Double>> posuny;

  public AsonanceVsechnyPosuny(ArrayList<String> texty) {
    super(texty);

    run();
  }

  @Override
  public void run() {
   
    ArrayList<IAsonance.SpocitaniAsonance> al = new ArrayList<>();

    for (String s : texty) {
      al.add(new SpocitaniAsonance(s));
      try {
        al.get((al.size()-1)).join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    boolean hotovo;
    do {
      hotovo = true;
      for (IAsonance.SpocitaniAsonance sa : al){
        if (!sa.isHotovo()) {
          hotovo = false;
        }
      }
    } while (!hotovo);

    int max = getTheLongest(al);

    ArrayList<ArrayList<Double>> posuny = new ArrayList<>(max);
    for (int i = 0; i < max; i++) {
      posuny.add(new ArrayList<Double>(texty.size()));
    }

    for (int i = 0; i < posuny.size(); i++) {
      ArrayList<Double> posun = posuny.get(i);
      for (int j = 0; j < al.size(); j++) {
        if (al.get(j).getHodnoty().size() > (i)) {
          posun.add(al.get(j).getHodnoty().get(i));
        } 
      }
      aTest.add(posun, "Posun o " + (i+1));
    }

  }

  /**
   * Spustí vlákno pro každý text, ve kterém vytvoří asonanci
   */
  private class SpocitaniAsonance extends IAsonance.SpocitaniAsonance {

    public SpocitaniAsonance(String text) {
      super(text);
      run();
      hotovo = true;
    }

    @Override
    public void run() {
      ArrayList<Character> vokaly = dejVokaly(text.toCharArray());
      int pocetKroku = (vokaly.size() / 2) + 1;

      ArrayList<Character> posun = new ArrayList<>(vokaly);

      for (int i = 1; i <= 15; i++) {

        //..začátek posunu naplnim null
        posun.add(0, null);

        int pocetStejnych = 0;
        //..projdeme vokály a porovnáme s posunem
        for (int x = 0; x < vokaly.size(); x++) {
          if (vokaly.get(x).equals(posun.get(x))) {
            pocetStejnych++;
          }
        }
        //..máme počet stejných.
        //.. spočítáme porovnávané
        int pocetPorovnavanych = vokaly.size() - (i);

        //.. kolik to je
        double pomer = ((double) pocetStejnych / (double) pocetPorovnavanych);

        //.. vložím průměr do ArrayListu
        hodnoty.add(pomer);
      }
    }
  }
}
